//
//  CityTypeController.m
//  Zyachting
//
//  Created by NGUYEN THE HUNG on 10/29/12.
//
//

#import "CityTypeController.h"
#import "CitySelectViewController.h"
#import "SearchViewController.h"
@interface CityTypeController ()

@end

@implementation CityTypeController
@synthesize tableView,currenChooseType,searchViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"Choose Region";
    UIBarButtonItem* left = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelBtnTapped:)];
    [self.navigationItem setLeftBarButtonItem:left];
    [left release];
    
    UIBarButtonItem* right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneBtnTapped:)];
    [self.navigationItem setRightBarButtonItem:right];
    [right release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)doneBtnTapped:(id)sender
{
    [self.searchViewController.selectedDict setObject:[NSNumber numberWithInteger:self.currenChooseType] forKey:@"city"];
    [self.searchViewController.table reloadData];
    [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
    
}
-(IBAction)cancelBtnTapped:(id)sender
{
    [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark -
#pragma mark TableView DataSource Method
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 3;
            break;
        case 2:
        {
            NSString* filePath = [[NSBundle mainBundle] pathForResource:@"contents" ofType:@"plist"];
            NSArray* cities = [[NSMutableDictionary dictionaryWithContentsOfFile:filePath] objectForKey:@"cities"];
            return cities.count-4;
        }
        default:
            break;
    }
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    [cell setAccessoryType:UITableViewCellAccessoryNone];
    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = @"Any city and region";
            if (self.currenChooseType == 0) {
                [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
            }
            break;
        case 1:
        {
            NSString* filePath = [[NSBundle mainBundle] pathForResource:@"contents" ofType:@"plist"];
            NSArray* cities = [[NSMutableDictionary dictionaryWithContentsOfFile:filePath] objectForKey:@"cities"];
            NSArray* sub = [cities subarrayWithRange:NSMakeRange(1, 3)];
            cell.textLabel.text = [sub objectAtIndex:indexPath.row];
            if (self.currenChooseType>=1 && self.currenChooseType <=3 && self.currenChooseType == indexPath.row+1) {
                [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
                
            }
        }
            break;
        case 2:
        {
            NSString* filePath = [[NSBundle mainBundle] pathForResource:@"contents" ofType:@"plist"];
            NSArray* cities = [[NSMutableDictionary dictionaryWithContentsOfFile:filePath] objectForKey:@"cities"];
            NSArray* sub = [cities subarrayWithRange:NSMakeRange(4, cities.count-4)];
            cell.textLabel.text = [sub objectAtIndex:indexPath.row];
            if (self.currenChooseType>3 && self.currenChooseType == indexPath.row +4) {
                [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
            }
        }
          
            break;
        default:
            break;
    }

    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    return cell;
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return @"Any";
            break;
        case 1:
            return   @"Costa Blanca North";
            break;
        case 2:
            return   @"Costa Blanca South";
            
        default:
            break;
    }
    return nil;
}
#pragma mark -
#pragma mark TableView Delegate Method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch ([indexPath section]) {
        case 0:
            self.currenChooseType = 0;
            break;
        case 1:
            self.currenChooseType = 1 + indexPath.row;
            break;
        case 2:
            self.currenChooseType = 4 + indexPath.row;
            break;
        default:
            break;
    }

    [self.tableView reloadData];

}

@end
